package nancal.mp.service


import nancal.mp.db.es.esr
import nbcp.base.comm.ListResult
import nbcp.base.db.IdName
import nbcp.base.db.annotation.Cn
import nbcp.base.extend.AsString
import nbcp.base.extend.Slice
import nbcp.myoql.db.comm.BaseMetaData
import org.springframework.stereotype.Component


@Component
class EsCrudService :ICrudService{
    override fun getGroups(): ListResult<String> {
        var ret = esr.groups .map {
                var name = it::class.java.simpleName
                return@map name.Slice(0, -5)

        }

        return ListResult.of(ret)
    }

    override fun getEntities(group: String): ListResult<IdName> {
        var group = group + "Group"
        var groupObj = esr.groups.firstOrNull { it::class.java.simpleName == group }
        if (groupObj == null) return ListResult.error("找不到group")

        groupObj.getEntities()
            .map {
                var remark = it::class.java.getAnnotationsByType(Cn::class.java).firstOrNull()?.value;

                var entName =  it.userDefineTableName
                return@map IdName(entName, remark.AsString(entName))
            }.apply { return ListResult.of(this) }
    }

    override fun getEntity(name: String): BaseMetaData<out Any> {
         return esr.getIndex(name)!!
    }

}

